package com.shamar.blog.service.impl;


import com.shamar.blog.dao.BlogCategoryDao;
import com.shamar.blog.dao.BlogContentDao;
import com.shamar.blog.dto.BlogContentExecution;
import com.shamar.blog.entity.BlogCategory;
import com.shamar.blog.entity.BlogContent;
import com.shamar.blog.enums.BlogContentStateEnum;
import com.shamar.blog.exceptions.BlogCategoryOperationException;
import com.shamar.blog.service.BlogContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class BlogContentServiceImpl implements BlogContentService {
	@Autowired
	private BlogContentDao blogContentDao;
	@Autowired
	private BlogCategoryDao blogCategoryDao;

	/**
	 * 根据blogContentCondition查询博文(博文列表)
	 * @param blogContentCondition
	 * @param pageIndex 第几页(n-1)
	 * @param pageSize	 每页记录数
	 * @return
	 */
	@Override
	public BlogContentExecution selectByBlogContentCondition(BlogContent blogContentCondition, int pageIndex, int pageSize) {
//		根据传入条件查询数据库
		BlogContentExecution blogContentExecution = new BlogContentExecution();
		List<BlogContent> blogContentList = blogContentDao.selectByBlogContentCondition(blogContentCondition,pageIndex,pageSize);
		if (!blogContentList.isEmpty()) {
			blogContentExecution.setBlogContentList(blogContentList);
		}
		return blogContentExecution;
	}

	/**
	 * 获取所有的博文
	 * @return
	 */
	@Override
	public BlogContentExecution getAllBlogContent() {
		try{
			//获得数据库中全部博文列表
			List<BlogContent> blogContentList = blogContentDao.getAllBlogContent();
			BlogContentExecution blogContentExecution = new BlogContentExecution();
			if (Objects.nonNull(blogContentList)) {
				blogContentExecution.setBlogContentList(blogContentList);
				blogContentExecution.setCount(blogContentList.size());
				return  blogContentExecution;
			}else {
//				数据库中没有数据
				return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
			}
			}
			catch (Exception e){
//			查询数据库异常
			return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
		}
	}

	/**
	 * 根据传入的blogContent实体新增博文
	 * @param blogContent
	 * @param status
	 * @return
	 */
	@Override
	@Transactional
	public BlogContentExecution addBlogContent(BlogContent blogContent,Integer status) {
		//空值判断
		if(blogContent == null){
			return new BlogContentExecution(BlogContentStateEnum.NULL_CONTENT_INFO);
		}
		try{
		//给博文信息赋初值
			blogContent.setCreatetime(new Date());
			blogContent.setLastEdittime(new Date());
			blogContent.setStatus(status);
			if (status == 1){
				//将要正式发布,更改pushtime
				blogContent.setPushtime(new Date());
			}
			int effectedNum = blogContentDao.insertSelective(blogContent);
			if (effectedNum <= 0){
				//和数据库交互失败
				return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
			}
			return new BlogContentExecution(BlogContentStateEnum.SUCCESS);
		}catch (Exception e){
			throw new BlogCategoryOperationException("添加博文失败:"+e.getMessage());
		}
	}

	@Override
	public BlogContentExecution deleteByPrimaryKey(Integer id) {
		if (Objects.nonNull(id) && id != 0){
			int effectedNum = blogContentDao.deleteByPrimaryKey(id);
			if (effectedNum <= 0){
				return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
			}
			else{
				return new BlogContentExecution(BlogContentStateEnum.SUCCESS);
			}

		}
		return new BlogContentExecution(BlogContentStateEnum.NULL_CONTENT_INFO);
	}

	/**
	 * 根据传入的index判断要有条件获得的博文列表,index为取值条件,如最近,最受欢迎
	 * @param index
	 * @return
	 */
	@Override
	public BlogContentExecution getIndexContent(Integer index) {
		//index为1,则为取最近发表的博文
		if (index == 1){
		//获得所有的博文
		List<BlogContent> blogContentList = blogContentDao.getAllBlogContent();
		int size = blogContentList.size();
		for(int i=size-1;i>=0;i--){
			//删除未发布的博文
			if(blogContentList.get(i).getPushtime() == null){
				blogContentList.remove(i);
			}
		}
		size = blogContentList.size();
		for(int i=size-1;i>=0;i--){
			if(blogContentList.get(i).getStatus() == 2){
				blogContentList.remove(i);
			}
		}
		blogContentList.sort((o1, o2) -> {
			//按照BlogContent的pushtime字段进行降序排列
			return Integer.compare(0, o1.getPushtime().compareTo(o2.getPushtime()));
		});
		if(blogContentList.size() > 6){
			//截取前六条记录
			blogContentList = blogContentList.subList(0,6);
		}
		BlogContentExecution blogContentExecution = new BlogContentExecution(BlogContentStateEnum.SUCCESS);
		blogContentExecution.setBlogContentList(blogContentList);
		return blogContentExecution;
	}
	else if (index == 2){
		//获得收藏最多的博文
		List<BlogContent> blogContentList = blogContentDao.getAllBlogContent();
		int size = blogContentList.size();
		for(int i=size-1;i>=0;i--){
			//删除未发布的博文
			if(blogContentList.get(i).getStatus() == 2){
				blogContentList.remove(i);
			}
		}
		blogContentList.sort((o1, o2) -> {
			//按照BlogContent的star_count字段进行降序排列
			if (o1.getStarCount() < o2.getStarCount()) {
				return 1;
			} else if (o1.getStarCount().equals(o2.getStarCount())) {
				return 0;
			}
			return -1;
		});
		if(blogContentList.size() > 6){
			//截取前六条记录
			blogContentList = blogContentList.subList(0,6);
		}
		BlogContentExecution blogContentExecution = new BlogContentExecution(BlogContentStateEnum.SUCCESS);
		blogContentExecution.setBlogContentList(blogContentList);
		return blogContentExecution;
	}
	return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
	}
    /**
     * 根据参数查询博文 参数包括 博文添加,排序的字段,排序的方式,limit 0,10
     * @param blogContentCondition
     * @param orderParam
     * @param orderWay
     * @param rowIndex
     * @param pageSize
     * @return
     */
    @Override
    public BlogContentExecution selectByBlogContentConditionPagination(BlogContent blogContentCondition, String orderParam, String orderWay, int rowIndex, int pageSize) {
    	if(Objects.isNull(blogContentCondition)) {
    		blogContentCondition = new BlogContent();
		}else if(Objects.isNull(orderParam)) {
    		orderParam = "star_count";
		}else if(Objects.isNull(orderWay)) {
    		orderWay = "DESC";
		}
        List<BlogContent> blogContentList = blogContentDao.selectByBlogContentConditionPagination(blogContentCondition, orderParam, orderWay, rowIndex, pageSize);
        if (Objects.nonNull(blogContentList)) {
        BlogContentExecution blogContentExecution = new BlogContentExecution(BlogContentStateEnum.SUCCESS);
        blogContentExecution.setBlogContentList(blogContentList);
        return blogContentExecution;
        }
        return new BlogContentExecution();
    }

	@Override
	public BlogContentExecution selectByPrimaryKey(Integer id) {
		if (Objects.isNull(id)) {
			return new BlogContentExecution(BlogContentStateEnum.NULL_CONTENT_INFO);
		}
		try{
		BlogContent blogContent = blogContentDao.selectByPrimaryKey(id);
		if (Objects.nonNull(blogContent)) {
			//获取分类id
			Integer cid = blogContent.getCategoryId();
			BlogCategory blogCategory = blogCategoryDao.selectByPrimaryKey(cid);
			blogContent.setCategoryName(blogCategory.getCategoryName());
			BlogContentExecution blogContentExecution = new BlogContentExecution();
			blogContentExecution.setBlogContent(blogContent);
			return blogContentExecution;
		//通过分类id查询分类名
		}else {
			return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
		}
		}catch (Exception e){
			return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
		}
	}

	/**
	 * 根据传入的BlogContent实体更新相应的记录
	 * @param blogContent
	 * @return
	 */
	@Override
	@Transactional
	public BlogContentExecution updateBlogContent(BlogContent blogContent) {
		if (blogContent == null) {
			return new BlogContentExecution(BlogContentStateEnum.NO_BLOG);
		}
		try{
			blogContent.setLastEdittime(new Date());
			int effectedNum =  blogContentDao.updateByPrimaryKeySelective(blogContent);
			if (effectedNum <= 0){
				//数据库错误
				return new BlogContentExecution(BlogContentStateEnum.DATABASE_ERROR);
			}else{
				//操作成功
				return new BlogContentExecution(BlogContentStateEnum.SUCCESS);
			}
		}catch (Exception e){
			throw new BlogCategoryOperationException("更新失败" + e.getMessage());
		}
	}
}
